package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.Arrays;

@WebServlet("/FormServlet")
public class FormServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        // 获取表单数据
        String username = request.getParameter("username");
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        String confirmPassword = request.getParameter("confirmPassword");
        String phone = request.getParameter("phone");
        String birthdate = request.getParameter("birthdate");
        String gender = request.getParameter("gender");
        String[] hobbies = request.getParameterValues("hobbies");
        String education = request.getParameter("education");
        String address = request.getParameter("address");

        // 验证密码
        if (!password.equals(confirmPassword)) {
            request.setAttribute("error", "两次输入的密码不一致！");
            request.getRequestDispatcher("form-demo.jsp").forward(request, response);
            return;
        }

        // 验证手机号
        if (phone != null && !phone.isEmpty() && !phone.matches("[0-9]{11}")) {
            request.setAttribute("error", "请输入正确的手机号码！");
            request.getRequestDispatcher("form-demo.jsp").forward(request, response);
            return;
        }

        // 验证邮箱
        if (email != null && !email.isEmpty() && !email.matches("^[A-Za-z0-9+_.-]+@(.+)$")) {
            request.setAttribute("error", "请输入正确的邮箱地址！");
            request.getRequestDispatcher("form-demo.jsp").forward(request, response);
            return;
        }

        // 保存到session
        HttpSession session = request.getSession();
        session.setAttribute("formData", new FormData(
                username, email, phone, birthdate, gender,
                hobbies != null ? Arrays.asList(hobbies) : null,
                education, address));

        // 显示成功消息
        request.setAttribute("message", "表单提交成功！");
        request.getRequestDispatcher("form-demo.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    // 用于存储表单数据的内部类
    private static class FormData {
        private final String username;
        private final String email;
        private final String phone;
        private final String birthdate;
        private final String gender;
        private final java.util.List<String> hobbies;
        private final String education;
        private final String address;

        public FormData(String username, String email, String phone, String birthdate,
                String gender, java.util.List<String> hobbies, String education, String address) {
            this.username = username;
            this.email = email;
            this.phone = phone;
            this.birthdate = birthdate;
            this.gender = gender;
            this.hobbies = hobbies;
            this.education = education;
            this.address = address;
        }

        // Getters
        public String getUsername() {
            return username;
        }

        public String getEmail() {
            return email;
        }

        public String getPhone() {
            return phone;
        }

        public String getBirthdate() {
            return birthdate;
        }

        public String getGender() {
            return gender;
        }

        public java.util.List<String> getHobbies() {
            return hobbies;
        }

        public String getEducation() {
            return education;
        }

        public String getAddress() {
            return address;
        }
    }
}